package com.zby.charity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zby.charity.common.BaseResponse;
import com.zby.charity.common.ErrorCode;
import com.zby.charity.common.PageRequest;
import com.zby.charity.common.ResultUtils;
import com.zby.charity.constant.UserConstant;
import com.zby.charity.dao.UserDao;
import com.zby.charity.entity.Dto.LoginUser;
import com.zby.charity.entity.UserEntity;
import com.zby.charity.entity.vo.*;
import com.zby.charity.exception.BusinessException;
import com.zby.charity.service.UserService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Stream;

/**
 * @author zby
 * @email 2069189112@qq.com
 * @date 2024-12-26 16:38:16
 */
@Slf4j
@RestController
@RequestMapping("charity/user")
public class UserController {
    private static Map<String, String> userDatabase = new HashMap<>();
    @Autowired
    public UserDao userDao;
    @Autowired
    public UserService userService;

    @GetMapping("/list")
    public BaseResponse<List<UserEntity>> list() {
        List<UserEntity> list = userDao.selectList(null);
        for (UserEntity userEntity : list)
            System.out.println(userEntity);
        return ResultUtils.success(list);
    }

    @PostMapping("/register")
    public BaseResponse<Long> register(@RequestBody UserRegisterRequest user) {
        log.info("用户" + user.getAccount() + "进行注册！");
        if (user == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String name = "昵称" + new Random().nextInt(1000);
        String account = user.getAccount().trim();
        String password = user.getPassword().trim();
        String checkPassword = user.getCheckPassword().trim();
        if (account.contains(" ")) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "账号不能包含空格");
        }
        if (account.contains(" ")) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码不能包含空格");
        }
        if (StringUtils.isAnyBlank(account, password, checkPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        long result = userService.userRegister(name, account, password, checkPassword);
        return ResultUtils.success(result);
    }

    @PostMapping("/login")
    public BaseResponse<LoginUser> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        LoginUser loginUserVO = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(loginUserVO);
    }

    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        boolean result = userService.userLogout(request);
        return ResultUtils.success(result);
    }

    @PostMapping("/get/login")
    public BaseResponse<UserEntity> getLoginUser(HttpServletRequest request) {
        UserEntity user = userService.getLoginUser(request);
        return ResultUtils.success(user);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateUser(@RequestBody UserEntity user, HttpServletRequest request) {
        if (user == null || user.getId() == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        UserEntity loginUser = userService.getLoginUser(request);
        String name = user.getName();
        String account = user.getAccount();
        String password = user.getPassword();
        if (StringUtils.isAnyBlank(name, password,account))
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        Integer role = user.getRole();
        if (role >= 2 || role < 0)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        if (!user.getId().equals(loginUser.getId())) {
            boolean b = userService.updateById(user);
            if (!b)
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        } else {
            request.getSession().setAttribute(UserConstant.USER_LOGIN_STATE, user);
            boolean b = userService.updateById(user);
            if (!b)
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestBody Integer id, HttpServletRequest request) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserEntity loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        boolean b = userService.removeById(id);
        return ResultUtils.success(b);
    }

    @PostMapping("/page/list")
    public BaseResponse<Page<UserEntity>> listUserByPage(@RequestBody PageRequest
                                                                 userQueryRequest, HttpServletRequest request) {
        UserEntity loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        long current = userQueryRequest.getCurrent();
        long size = userQueryRequest.getPageSize();
        if (current <= 0 || size <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<UserEntity> userPage = userService.page(new Page<>(current, size),
                null);
        return ResultUtils.success(userPage);
    }

    @GetMapping("/get/userById")
    public BaseResponse<UserEntity> getUserById(@RequestParam("id") int id, HttpServletRequest request) {
        UserEntity loginUser = userService.getLoginUser(request);
        if (loginUser == null)
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        if (id <= 0)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        UserEntity byId = userService.getById(id);
        if (byId == null)
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(byId);
    }

}
